<?php
/*
+-----------------------------------------------------------------------------+
| $Id: admin_language.php 2010-05-24 12:37:44Z Bleakwind $
| Manage admin language
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GNU Lesser General Public License Version 3 (LGPLv3):
|   http://www.gnu.org/licenses/lgpl.html
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

// Admin group add and amend ajax
function submit_form($type, $value, $submit)
{
    global $LANGUAGE,$LANGLIST,$SETTING,$CONFIGURE,$MEMBER,$ADMIN,$db,$sys,$c,$setting_cover,$setting_attach;
    $ajax_response = new xajaxResponse();
    $error = false;
    //$ajax_response->alert(print_r($value, true)); $ajax_response->assign($submit,"disabled",false); return $ajax_response;

    if ($type == "amend") {
        $submit_value   = "编辑";
    } else {
        $submit_value   = "添加";
    }

    if( !preg_match("/^[a-z0-9_]+$/i",$value['name']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "name"; }
        $ajax_response->assign("name_return", "innerHTML", "<span class=\"prompt_failed\">名称必须填写,且只能为英文,数字或下划线<!-- name error --></span>");
    } else {
        $ajax_response->assign("name_return", "innerHTML", "");
    }

    if( !preg_match("/^[a-z0-9]+$/i",$value['code']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "code"; }
        $ajax_response->assign("code_return", "innerHTML", "<span class=\"prompt_failed\">代码不能为空且只能为英文数字<!-- code error --></span>");
    } else {
        $ajax_response->assign("code_return", "innerHTML", "");
    }

    if( !preg_match("/^[a-z0-9_.]+$/i",$value['icon']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "icon"; }
        $ajax_response->assign("icon_return", "innerHTML", "<span class=\"prompt_failed\">图标不能为空且只能为英文数字下划线和点<!-- icon error --></span>");
    } else {
        $ajax_response->assign("icon_return", "innerHTML", "");
    }

    if( !preg_match("/^[a-z0-9_]+$/i",$value['dir']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "dir"; }
        $ajax_response->assign("dir_return", "innerHTML", "<span class=\"prompt_failed\">排序不能为空且只能为英文数字和下划线<!-- dir error --></span>");
    } else {
        $ajax_response->assign("dir_return", "innerHTML", "");
    }

    if(!preg_match("/^[0-9]+$/",$value['rank'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "rank"; }
        $ajax_response->assign("rank_return", "innerHTML", "<span class=\"prompt_failed\">排序不能为空且必须为数字<!-- rank error --></span>");
    } else {
        $ajax_response->assign("rank_return", "innerHTML", "");
    }

    if($error){
        $ajax_response->assign($submit,"value",$submit_value);
        $ajax_response->assign($submit,"disabled",false);
        $ajax_response->redirect("#".$anchor);
    }else{

        if ($type == "amend") {

            if (!preg_match("/^[0-9]+$/",$sys->get['admin_language_id'])){
                $ajax_response->alert("用户ID错误!");
                $ajax_response->assign($submit,"value",$submit_value);
                $ajax_response->assign($submit,"disabled",false);
            } else {

                $total_record = func::db_count_record(DB_TABLE_ADMIN_LANGUAGE, "name='".addslashes($value['name'])."' AND id!=".(int)$sys->get['admin_language_id']);
                if ($total_record > 0) {
                    $ajax_response->alert("此语言名称已经存在,请重新填写!");
                    $ajax_response->assign($submit,"value",$submit_value);
                    $ajax_response->assign($submit,"disabled",false);
                } else {

                    $total_record = func::db_count_record(DB_TABLE_ADMIN_LANGUAGE, "code='".addslashes($value['code'])."' AND id!=".(int)$sys->get['admin_language_id']);
                    if ($total_record > 0) {
                        $ajax_response->alert("此语言代码已经存在,请重新填写!");
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    } else {
                    
                        $sql = "UPDATE ".DB_TABLE_ADMIN_LANGUAGE." SET
                                    name    = '".addslashes($value['name'])."',
                                    brief   = '".addslashes($value['brief'])."',
                                    code    = '".addslashes($value['code'])."',
                                    icon    = '".addslashes($value['icon'])."',
                                    dir     = '".addslashes($value['dir'])."',
                                    rank    = '".addslashes($value['rank'])."'
                                WHERE id=".$sys->get['admin_language_id']."";
                        $result = $db->Execute($sql);
                        if (!$result) {
                            $ajax_response->alert($db->ErrorMsg());
                            $ajax_response->assign($submit,"value",$submit_value);
                            $ajax_response->assign($submit,"disabled",false);
                        }else{
                            $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=admin_language&pag=".$sys->get['pag']);
                        }
                    }
                }
            }

        } else {

            $total_record = func::db_count_record(DB_TABLE_ADMIN_LANGUAGE, "name='".addslashes($value['name'])."'");
            if ($total_record > 0) {
                $ajax_response->alert("此语言名称已经存在,请重新填写!");
                $ajax_response->assign($submit,"value",$submit_value);
                $ajax_response->assign($submit,"disabled",false);
            } else {

                $total_record = func::db_count_record(DB_TABLE_ADMIN_LANGUAGE, "code='".addslashes($value['code'])."'");
                if ($total_record > 0) {
                    $ajax_response->alert("此语言代码已经存在,请重新填写!");
                    $ajax_response->assign($submit,"value",$submit_value);
                    $ajax_response->assign($submit,"disabled",false);
                } else {

                    $sql = "INSERT INTO ".DB_TABLE_ADMIN_LANGUAGE." SET
                               name     = '".addslashes($value['name'])."',
                               brief    = '".addslashes($value['brief'])."',
                               code     = '".addslashes($value['code'])."',
                               icon     = '".addslashes($value['icon'])."',
                               dir      = '".addslashes($value['dir'])."',
                               rank     = '".addslashes($value['rank'])."'";
                    $result = $db->Execute($sql);
                    $last_record = $db->Insert_ID();
                    if (!$result) {
                        $ajax_response->alert($db->ErrorMsg());
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    }else{
                        $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=admin_language&pag=".$sys->get['pag']);
                    }
                }
            }
        }
    }


    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "submit_form");
//////

if($sys->get['ope'] == "add"){



}elseif($sys->get['ope'] == "amend"){

    if (!preg_match("/^[0-9]+$/",$sys->get['admin_language_id'])){
        $sys->prompt("failed","非法操作!<!-- admin_language_id error -->");
    }else{

        $sql = "SELECT *
                FROM ".DB_TABLE_ADMIN_LANGUAGE."
                WHERE id=".$sys->get['admin_language_id'];
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }else {
            if(!$result->EOF){
                $admin_language_detail = array(
                    "id"    => $result->fields['id'],
                    "name"  => $result->fields['name'],
                    "brief" => $result->fields['brief'],
                    "code"  => $result->fields['code'],
                    "icon"  => $result->fields['icon'],
                    "dir"   => $result->fields['dir'],
                    "rank"  => $result->fields['rank'],
                );
            }
		}
    }
    $t->assign("admin_language_detail",$admin_language_detail);

}elseif($sys->get['ope'] == "del"){

    // check the post
    if (!preg_match("/^[0-9]+$/",$sys->get['admin_language_id'])){
        $sys->prompt("failed","非法操作!<!-- admin_language_id error -->");
    }else{

        $sql = "DELETE FROM ".DB_TABLE_ADMIN_LANGUAGE."
                WHERE id=".$sys->get['admin_language_id'];
        $result = $db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }

        $sys->prompt("jump",$CONFIGURE['common']['control_admin']."?act=admin_language&pag=".$sys->get['pag']);
    }

}else{

    $total_record = func::db_count_record(DB_TABLE_ADMIN_LANGUAGE);
    $bwpage->set_record_listnum("20");
    $bwpage->set_record($total_record);
    $bwpage->page($CONFIGURE['common']['control_admin']."?act=admin_language&pag=%s");
    $sql = "SELECT *
            FROM ".DB_TABLE_ADMIN_LANGUAGE."
            ORDER BY rank, id
            LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
    $result = &$db->Execute($sql);
    if (!$result) {
        echo $db->ErrorMsg();
    }else {
        while(!$result->EOF){
            $admin_language_list[$result->fields['id']] = array(
                "id"    => $result->fields['id'],
                "name"  => $result->fields['name'],
                "brief" => $result->fields['brief'],
                "code"  => $result->fields['code'],
                "icon"  => $result->fields['icon'],
                "dir"   => $result->fields['dir'],
                "rank"  => $result->fields['rank'],
            );
			$result->MoveNext(); 
        }
    }
    $t->assign(array(
        "admin_language_list" => $admin_language_list,
        "pagination"    => $bwpage->pagination,
    ));
}
?>
